package application;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class XmlEncodeToFile {

	public static void writeToFile(HighScoreTable scoreTable){
		
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream("score_table.xml");
		} catch (FileNotFoundException e) {
			System.out.println("Could not open file!" + "score_table.xml");
			e.printStackTrace();
		}
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(fos);
			oos.writeObject(scoreTable);
			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	public static HighScoreTable readFromFile(){
		
		HighScoreTable scoreTable = null;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream("score_table.xml");
		} catch (FileNotFoundException e) {
			System.out.println("Could not open file!" + "score_table.xml");
			e.printStackTrace();
		}
		ObjectInputStream oos;
		try {
			oos = new ObjectInputStream(fis);
			scoreTable = (HighScoreTable) oos.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		}
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return scoreTable;
	}
}
